package project2;

public class CarSystem {
    Car[] che=new Car[]{new Jiaoche("皖A4546","大众",200,5),
            new Jiaoche("皖A6666","奔驰",800,5),
            new Jiaoche("皖A7788","宝马",800,5),
            new Keche("皖A4646","金杯",1000,23),
            new Keche("皖A1122","小金杯",1200,30),
            new Keche("皖A5566","大金杯",2000,50)};
    yonghu[] kh=new yonghu[10];
    private int count=6,count2;
//查看车辆信息
    public boolean caiGou(Car c){
        if(count>=che.length){//扩大数组
            Car[] newche=new Car[che.length+10];
            for(int i=0;i<count;i++){
                newche[i]=che[i];//旧数组的值带入新数组里面
            }
            che=newche;
        }
        for(int i=0;i<count;i++){
            if(che[i].getChePaiHao().equals(c.getChePaiHao())){
                return false;
            }
        }
        che[count++]=c;
        return true;
    }


//租车
    public Car zhuChe(String pai,String name){
        int i=this.zhao(pai);
        if(i>=0 && che[i].getZhuangTai().equals("当前可租")){
            kh[count2]=new yonghu();
            for(int j=count2-1;j>=0;j--){
                if(kh[j].getName().equals(name)){
                    kh[count2].setCiShu(kh[j].getCiShu()+1);
                    break;
                }
            }
            kh[count2].setName(name);
            kh[count2].setCheLiang(pai);
            count2++;
            che[i].setZhuangTai("已租出");
            return che[i];
        }
        return null;
    }

    public void showSChe(String xh){
        for(int i=0;i<count;i++){
            if(xh.equals("客车") &&che[i] instanceof Keche &&che[i].getZhuangTai().equals("当前可租")){
                che[i].show();
            }
            if(xh.equals("轿车") &&che[i] instanceof Jiaoche &&che[i].getZhuangTai().equals("当前可租")){
                che[i].show();
            }
        }
    }

    public void showkh(){
        for(int i=0;i<count2;i++){
            kh[i].show();
        }
    }

    public void show(){
        for(int i=0;i<count;i++){
            che[i].show();
        }
    }

    public boolean huan(String pai){
        int i=this.zhao(pai);
        if(i>=0 && che[i].getZhuangTai().equals("已租出")){
            che[i].setZhuangTai("当前可租");
            return true;
        }
        return false;
    }


    public int zhao(String pai){
        for(int i=0;i<count;i++){
            if(che[i].getChePaiHao().equals(pai)){
                return i;
            }
        }
        return -1;
    }

    public boolean baoFei(String pai){
        int i=this.zhao(pai);
        if(che[i].getZhuangTai().equals("当前可租")){
            for(int j=i;j<che.length-1;j++){
                che[j]=che[j+1];
                che[j+1]=null;
            }
            count--;
            return true;
        }
        return false;
    }
}
